'use strict';

const Service = require('../core/base_service');

class UserService extends Service {
  // 用户登录 / 注册
  async login(payload) {
    let res
    try {
      res = await this.ctx.model.User.find({ mobile: payload.mobile })
      if (this.ctx.helper.isEmpty(res)) { // 如果res为空 则没有该账号 直接去注册
        res = await this.ctx.model.User.create(payload)
        return this.success(res)
      } else { // 数据库中有此账号
        res = await this.ctx.model.User.find({
          mobile: payload.mobile,
          code: payload.code
        })
        // console.log('数据库中有此账号', ...res)
        if (this.ctx.helper.isEmpty(res)) {
          return this.fail(this.config.result.LOGIN_FAIL)
        } else {
          // console.log('res', res)
          //生成token
          // var au = await this.ctx.service.token.newGenerate(res)
          return this.success(...res)
        }
      }
    } catch (error) {
      return this.fail(this.config.result.EXCEPTION, error.message)
    }
  }
  // 修改用户信息
  async update (payload) {
    let res
    try {
      let isExist = await this.ctx.model.User.findById(payload._id)
      if (this.ctx.helper.isEmpty(isExist)) {
        return this.fail(this.config.result.NOT_EXIST)
      } else {
        res = await this.ctx.model.User.findByIdAndUpdate(payload._id, payload) // 必须传id才能修改成功
        let signInRes = await this.ctx.model.SignIn.find({
          mobile: payload.mobile
        })
        console.log('signInRes', signInRes)
        for (let i = 0; i < signInRes.length; i++) {
          await this.ctx.model.SignIn.findByIdAndUpdate(signInRes[i]._id, {name: payload.name})
        }
        let leaveRes = await this.ctx.model.Leave.find({
          mobile: payload.mobile
        })
        console.log('leaveRes', leaveRes)
        for (let i = 0; i < leaveRes.length; i++) {
          await this.ctx.model.Leave.findByIdAndUpdate(leaveRes[i]._id, {name: payload.name})
        }
        // await this.ctx.model.SignIn.findByIdAndUpdate(payload.mobile, {name: payload.name})
        // await this.ctx.model.Leave.findByIdAndUpdate(payload.mobile, {name: payload.name})
        // console.log('findByIdAndUpdate', res)
        return this.success(res)
      }
    } catch (error) {
      console.log('error', error)
    }
  }

  // 删除
  async deleteUser (payload) {
    let res
    try {
      res = await this.ctx.model.User.findByIdAndRemove(payload._id)
      return this.success(res)
    } catch (error) {
      console.log(error)
    }
  }

  // 获取全部用户
  async getUser (params) {
    let res
    try {
      res = await this.ctx.model.User.find(params)
      // res = await this.ctx.model.User.find(params)
      return this.success(res)
    } catch (error) {
      console.log(error)
    }
  }
  // 获取全部用户
  async getUsermohu (params) {
    let res
    console.log('params', params)
    try {
      if (params) {
        res = await this.ctx.model.User.find({ name: { $regex: params.name } })
      } else {
        res = await this.ctx.model.User.find()
      }
      
      // res = await this.ctx.model.User.find(params)
      return this.success(res)
    } catch (error) {
      console.log(error)
    }
  }
  // 获取全部用户
  async getUserPaging (params) {
    let res
    try {
      res = await this.ctx.model.User.find()
      console.log('params', params)
      const {currentPage, pageSize} = params
      // console.log('res', res)
      // 总条数
      let count = await this.ctx.model.User.find().count()
      // 总页数
      let totalPage = Math.ceil(count/pageSize)
      console.log('count', count)
      return this.success(res)
    } catch (error) {
      console.log(error)
    }
  }
}

module.exports = UserService;
